﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TopBuilds.Restaurant.Data;
using TopBuilds.Restaurant.ViewModel;

namespace TopBuilds.Restaurant.ManagementClient
{
    /// <summary>
    /// EmployeeManagement.xaml 的交互逻辑
    /// </summary>
    public partial class EmployeeManagement : Page
    {
        public EmployeeManagement()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)  //增加
        {
          
            string privilege_name = privilegeIdtext.Text;
            string department_name = departmentIdtext.Text;
            string name = nametext.Text;
            string password = passwordtext.Password;

            if (privilege_name != null && privilege_name.Length > 0 && department_name != null && department_name.Length > 0 && name != null && name.Length > 0 && password != null && password.Length > 0)
            {
                Data.Department department = Data.DepartmentObject.GetDepartment(department_name);
                Data.Privilege privilege = Data.PrivilegeObject.GetPrivilege(privilege_name);

                int privilegeId = privilege.Id;
                int departmentId = department.Id;

                Employee employee = new Employee(privilegeId, departmentId, name, password);

                if (EmployeeObject.Add(employee) == -1)
                {
                    MessageBox.Show("增加失败");
                    return;
                }

                #region re-binding
                //listBox1.ItemsSource = null;
                //listBox1.ItemsSource = t;

                // re-bind for TextBox
                Binding PrivilegeId_binding = new Binding("PrivilegeId");
                PrivilegeId_binding.Converter = this.FindResource("privilegeConverter") as PrivilegeValueConverter;
                PrivilegeId_binding.Mode = BindingMode.OneWay;
                privilegeIdtext.SetBinding(ComboBox.TextProperty, PrivilegeId_binding);

                Binding DepartmentId_binding = new Binding("DepartmentId");
                DepartmentId_binding.Mode = BindingMode.OneWay;
                departmentIdtext.SetBinding(ComboBox.TextProperty, DepartmentId_binding);

                Binding name_binding = new Binding("Name");
                name_binding.Mode = BindingMode.OneWay;
                nametext.SetBinding(TextBox.TextProperty, name_binding);

                Binding password_binding = new Binding("Password");
                password_binding.Mode = BindingMode.OneWay;
                passwordtext.SetBinding(TextBox.TextProperty, password_binding);
                #endregion

                /// reset the data from database
                EmployeeCollection t = listBox1.ItemsSource as EmployeeCollection;
                t.Add(employee);

                MessageBox.Show("增加成功");
            }
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e)  //更新
        {
            int selectedIndex = listBox1.SelectedIndex;
            if (selectedIndex >= 0)
            {
                EmployeeCollection t = listBox1.ItemsSource as EmployeeCollection;

                Employee employee = t[selectedIndex] as Employee;

                string privilege_name = privilegeIdtext.Text;
                string department_name = departmentIdtext.Text;

                Data.Department department = Data.DepartmentObject.GetDepartment(department_name);
                Data.Privilege privilege = Data.PrivilegeObject.GetPrivilege(privilege_name);

                int privilegeId = privilege.Id;
                int departmentId = department.Id;

                employee.PrivilegeId = privilegeId;
                employee.DepartmentId = departmentId;
                employee.Name = nametext.Text;
                employee.Password = passwordtext.Password;

                #region re-binding

                // re-binding for ListBox
                listBox1.ItemsSource = null;
                listBox1.ItemsSource = t;

                // re-bind for TextBox
                Binding PrivilegeId_binding = new Binding("PrivilegeId");
                PrivilegeId_binding.Mode = BindingMode.OneWay;
                privilegeIdtext.SetBinding(TextBox.TextProperty, PrivilegeId_binding);

                Binding DepartmentId_binding = new Binding("DepartmentId");
                DepartmentId_binding.Mode = BindingMode.OneWay;
                departmentIdtext.SetBinding(TextBox.TextProperty, DepartmentId_binding);

                Binding name_binding = new Binding("Name");
                name_binding.Mode = BindingMode.OneWay;
                nametext.SetBinding(TextBox.TextProperty, name_binding);

                Binding password_binding = new Binding("Password");
                password_binding.Mode = BindingMode.OneWay;
                passwordtext.SetBinding(TextBox.TextProperty, password_binding);

                #endregion

                if (EmployeeObject.Update(employee) == -1)
                {
                    MessageBox.Show("更新失败");
                }
            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e) //删除
        {
            int selectedIndex = listBox1.SelectedIndex;
            if (selectedIndex >= 0)
            {
                EmployeeCollection t = listBox1.ItemsSource as EmployeeCollection;
                int id = (t[selectedIndex] as Employee).Id;
                if (MessageBoxResult.Yes == MessageBox.Show("确定要删除么?", "警告", MessageBoxButton.YesNo))
                {
                    t.RemoveAt(selectedIndex);
                    #region re-binding
                    // re-bound for ListBox
                    //listBox1.ItemsSource = null;
                    //listBox1.ItemsSource = t;

                    // re-binding for TextBox
                    Binding PrivilegeId_binding = new Binding("PrivilegeId");
                    PrivilegeId_binding.Mode = BindingMode.OneWay;
                    privilegeIdtext.SetBinding(TextBox.TextProperty, PrivilegeId_binding);

                    Binding DepartmentId_binding = new Binding("DepartmentId");
                    DepartmentId_binding.Mode = BindingMode.OneWay;
                    departmentIdtext.SetBinding(TextBox.TextProperty, DepartmentId_binding);

                    Binding name_binding = new Binding("Name");
                    name_binding.Mode = BindingMode.OneWay;
                    nametext.SetBinding(TextBox.TextProperty, name_binding);

                    Binding password_binding = new Binding("Password");
                    password_binding.Mode = BindingMode.OneWay;
                    passwordtext.SetBinding(TextBox.TextProperty, password_binding);
                    #endregion

                    if (!EmployeeObject.Delete(id))
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }

       
    }
}
